package com.example.a.rxjava2test;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import rx.Observable;
import rx.Observer;
import rx.Subscriber;

public class MainActivity extends AppCompatActivity {

    private static final String tag = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //创建观察者
        Observer<String> observer = new Observer<String>() {
            @Override
            public void onCompleted() {
                Log.i("onCompleted ---> ", "完成");
            }

            @Override
            public void onError(Throwable e) {
                Log.i("onError ---> ", e.toString());
            }

            @Override
            public void onNext(String s) {
                Log.i("onNext ---> ", "Item: " + s);
            }
        };

        //创建被观察者
        Observable observable = Observable.create(new Observable.OnSubscribe<String>(){
            @Override
            public void call(Subscriber<? super String> subscriber) {
                subscriber.onNext("hello");
                subscriber.onNext("world");
                subscriber.onNext("byebye");
                subscriber.onCompleted();
            }
        });

        //被观察者注册观察者，类似Button.setOnClickListener(new View.OnClickListener())；
        observable.subscribe(observer);
    }
}
